package com.aaa.backsystem.controller;

import com.aaa.backsystem.entity.*;
import com.aaa.backsystem.service.PetrolStationService;
import com.aaa.backsystem.service.StaffService;
import com.aaa.backsystem.util.MyConstants;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * fileName:CheckController
 * description:审批（公安局端）
 * author:gyc
 * createTime:2020/7/25 20:11
 * version:1.0.0
 */
@Controller
@RequestMapping("/check")
public class CheckController extends BaseController {

    @Autowired
    private PetrolStationService petrolStationService;

    @Autowired
    private StaffService staffService;

    /**
     * 跳转加油站注册审批
     *
     * @return
     */
    @RequestMapping("toCheckPetrolStation")
    public String toCheckPetrolStation() {
        return "check/checkPetrolStation";
    }

    /**
     * 跳转到加油站职工审批
     *
     * @return
     */
    @RequestMapping("toCheckStaff")
    public String toCheckStaff() {
        return "check/checkStaff";
    }

    /**
     * 显示加油站基本信息
     *
     * @param page
     * @param limit
     * @param status
     * @param searchLegalPersonName
     * @param searchPhoneNumber
     * @return
     */
    @RequestMapping("/petrolStationListAndDeptName")
    @ResponseBody
    public LayuiTable petrolStationListAndDeptName(Integer page, Integer limit, String status, String searchLegalPersonName, String searchPhoneNumber) {
        LayuiTable table = new LayuiTable();

        Wrapper wrapper = new EntityWrapper();
        //多条件查询所需要的集合
        Map<String, Object> condition = new HashMap(16);

        if (null != status && !"".equals(status)) {
            wrapper.like("status", status);
            condition.put("status", status);
        }
        if (null != searchLegalPersonName && !"".equals(searchLegalPersonName)) {
            wrapper.like("legal_person_name", searchLegalPersonName);
            condition.put("legal_person_name", searchLegalPersonName);
        }
        if (null != searchPhoneNumber && !"".equals(searchPhoneNumber)) {
            wrapper.like("legal_person_phone", searchPhoneNumber);
            condition.put("legal_person_phone", searchPhoneNumber);
        }


        //查询加油站的条数
        int petrolStationCount = petrolStationService.selectCount(wrapper);

        //如果表中没有数据。则不进行分页查询
        if (petrolStationCount > 0) {
            //分页
            Page<PetrolStationVo> pageInfo = new Page(page, limit);

            List<PetrolStationVo> petrolStationVoList = petrolStationService.findAllPetrolStationAndDeptName(pageInfo, condition);
            //从分页结果中提取list集合
            table.setCode(MyConstants.OPERATION_SUCCESS_CODE);
            table.setMsg(MyConstants.OPERATION_SUCCESS_MESSAGE);
            table.setData(petrolStationVoList);
            table.setCount(petrolStationCount);
        }
        return table;
    }

    /**
     * 更新加油站审核状态
     *
     * @return
     */
    @RequestMapping("/updatePetrolStationStatus")
    @ResponseBody
    public Result updatePetrolStationStatus(PetrolStation petrolStation) {

        if(petrolStation.getStatus()==0){
            petrolStation.setStatus(1);
        }else{
            petrolStation.setStatus(0);
        }

        boolean flag = petrolStationService.updateById(petrolStation);
        if (flag) {
            return success();
        }
        return error();
    }

    /**
     * 显示员工列表和部门名称
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/staffList")
    @ResponseBody
    public LayuiTable staffList(Integer page, Integer limit, String status, String searchStaffName, String searchStaffCard) {
        LayuiTable table = new LayuiTable();

        Wrapper wrapper = new EntityWrapper();
        //多条件查询所需要的集合
        Map<String, Object> condition = new HashMap(16);

        if (null != status && !"".equals(status)) {
            wrapper.like("status",status);
            condition.put("status", status);
        }
        if (null != searchStaffName && !"".equals(searchStaffName)) {
            wrapper.like("staff_name",searchStaffName);
            condition.put("staff_name", searchStaffName);
        }
        if (null != searchStaffCard && !"".equals(searchStaffCard)) {
            wrapper.like("staff_card",searchStaffCard);
            condition.put("staff_card", searchStaffCard);
        }
        wrapper.eq("del_flag",0);
        condition.put("del_flag", 0);

        //查询存在员工的条数
        int staffCount = staffService.selectCount(wrapper);
        //如果表中没有数据。则不进行分页查询
        if (staffCount > 0) {
            //分页
            Page<StaffVo> pageInfo = new Page(page, limit);

            List<StaffVo> allStaffListAndDeptName = staffService.findAllStaffListAndDeptName(pageInfo,condition);
            System.out.println(allStaffListAndDeptName);
            //从分页结果中提取list集合
            table.setCode(MyConstants.OPERATION_SUCCESS_CODE);
            table.setMsg(MyConstants.OPERATION_SUCCESS_MESSAGE);
            table.setData(allStaffListAndDeptName);
            table.setCount(staffCount);
        }
        return table;
    }

    /**
     * 更新加油站职工审核状态
     *
     * @return
     */
    @RequestMapping("/updateStaffStatus")
    @ResponseBody
    public Result updateStaffStatus(Staff staff) {

        if(staff.getStatus()==0){
            staff.setStatus(1);
        }else{
            staff.setStatus(0);
        }

        boolean flag = staffService.updateById(staff);
        if (flag) {
            return success();
        }
        return error();
    }
}
